<template>
  <div class="loginForm">
    <!-- :model当前表单数据对象，只有绑定model，才能让表单元素进行具体属性绑定 -->
    <el-form :model="form" :rules="rules" ref="loginForm" class="form">
      <!-- 如果想要让元素使用指定的验证规则，则需要添加prop属性 -->
      <el-form-item type="text" clearable prop="username" label="用户名" class="form-item">
        <el-input placeholder="用户名/手机" v-model="form.username" @focus="clearValid('username')"></el-input>
      </el-form-item>
      <el-form-item type="text" clearable prop="password" label="密码" class="form-item">
        <el-input placeholder="请输入密码" v-model="form.password" @focus="clearValid('password')" 
        @keyup.13="handlesubmit"></el-input>
      </el-form-item>
      <p class="form-text">
        <nuxt-link to="#">忘记密码</nuxt-link>
      </p>
      <el-button
        class="submit"
        type="primary"
        round
        @click="handlesubmit"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "admin",
      },
      // rules定义在data中，它是一个对象
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^1\d{10}$|^\w{3,}$/,
            message: "请输入合法的用户名/手机号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handlesubmit() {
      console.log(this.form);
      this.$refs["loginForm"].validate((valid) => {
        console.log(valid); //true或false
        if (valid) {
          // 发送登录请求
          // this.$axios({
          //   method: "post",
          //   url: "/accounts/login",
          //   data: this.form,
          // })
          // .then((res) => {
          //   console.log(res); // 成功返回结果
          //   if (res.data.token) {
          //     this.$message.success("登陆成功");
          //     // 赋值的写法：this.$store.commit('仓库名称/mutations的方法名称'，你向传递的数据)
          //     this.$store.commit("user/setUserInfo", res.data);
          //     this.$router.push({ path: "/" });
          //   }
          // })
          // .catch((err) => {
          //   console.log(err); //错误返回结果
          //   this.$message.warning("登陆失败，请重新输入");
          // });

          this.$store
            .dispatch("user/login", this.form)
            .then((res) => {
              // 成功提示
              this.$message.success("登陆成功");
              setTimeout(() => {
                this.$router.push({ path: "/" });
              }, 1000);
            })
            .catch((err) => {
              console.log(err);
              this.$message.warning("登陆失败，请重新输入");
            });
        } else {
          this.$message.warning("用户输入不合法");
        }
      });
    },
    clearValid(propname) {
      // 清除指定的验证信息
      this.$refs.loginForm.clearValidate(propname);
    },
  },
};
</script>

<style lang="less" scoped>
.loginForm {
  padding: 25px;
  .form-item {
    margin-bottom: 20px;
  }

  .form-text {
    font-size: 12px;
    color: #409eff;
    text-align: right;
    line-height: 1;
  }

  .submit {
    width: 100%;
    margin-top: 10px;
  }
}
</style>